<!doctype html>
<html lang="en" class="fixed">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <title>消息列表</title>
    <link rel="apple-touch-icon" sizes="120x120" href="favicon/apple-icon-120x120.png">
    <link rel="icon" type="image/png" sizes="192x192" href="favicon/android-icon-192x192.png">
    <link rel="icon" type="image/png" sizes="32x32" href="favicon/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="favicon/favicon-16x16.png">
    <link rel="stylesheet" type="text/css"
          href="<?php xcho(loadSource('/assets/plugins/bootstrap/css/bootstrap.min.css')) ?>">
    <link rel="stylesheet" href="<?php xcho(loadSource('/assets/plugins/animate.css/animate.css')) ?>">
    <link rel="stylesheet" href="<?php xcho(loadSource('/assets/plugins/font-awesome/css/font-awesome.css')) ?>">
    <link rel="stylesheet"
          href="<?php xcho(loadSource('/assets/plugins/data-table/media/css/dataTables.bootstrap.min.css')) ?>">
    <link rel="stylesheet" href="<?php xcho(loadSource('/app/css/style.css')) ?>">
    <link rel="stylesheet" href="<?php xcho(loadSource('/app/css/app.css')) ?>">
    <link rel="stylesheet" href="<?php xcho(loadSource('/assets/css/new_main.css')) ?>">
    
    <style>
        .pagination{
            margin: 0;
        }
    </style>
</head>

<body><?php include_once TPL_PATH . 'public/nav.phtml'; ?>

<div class="wrap">
    <div class="page-body" style="padding: 0">
        <div class="content" style="margin: 0;background: #ececec">
            <div class="row ">
                <div class="col-sm-12">
                    <div class="panel">
                        <div class="panel-header clearfix">
                            <div class="top_btn fr">
                                <button class="btn btn-sub" id="read_messages">已读</button>
                                <button class="btn btn-primary" id="del_messages">批量删除</button>
                            </div>
                            <div class="btn-group ">
                                <a href="/index/message/index/is_read/0" class="btn btn-wide btn-o btn-info" id="notread">未读<span id="no_read_message" style="padding: 5px 10px;border-radius: 50%;color: red"><?php xcho($noMsg)   ?></span></a>
                                <a href="/index/message/index/is_read/1" class="btn btn-wide btn-o btn-success" id="messageall">全部<span id="all_message" style="padding: 5px 10px;border-radius: 50%;"><?php xcho($totalMsg)   ?></span></a>
                            </div>
                        </div>
                        <div class="panel-content">

                            <div class="table-responsive">
                                <table class="table table-striped table-hover">
                                    <thead>
                                    <tr>

                                        <th><span id="checkAll"><input value="1" type="checkbox" name="checkAll"></span></th>
                                        <th>内容</th>

                                        <th>消息时间</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>

                                <?php foreach ($list as $k=>$v) { ?>
                                    <tr data-r="<?php   xcho($v['id']) ?>">
                                        <td><input value="<?php   xcho($v['id']) ?>" type="checkbox" name="ck">&nbsp;&nbsp;</td>
                                        <td>
                                            <a href="<?php xcho(url('/index/message/messageinfo'))?>?message_id=<?php xcho($v['id']) ?>" >
                                            <?php if($v['read_time']==0||empty($v['read_time'])) { ?>
                                            <i class="fa fa-envelope color-primary" style="margin-right: 20px;"></i>
                                            <?php } ?>
                                            <?php   xcho($v['message']) ?>
                                            </a>
                                        </td>

                                        <td><?php   xcho(date('Y-m-d H:i:s',$v['add_time']))  ?></td>

                                        <td><a><span data-messageid="<?php  xcho($v['id']) ?>" class="del_message">删除</span></a><a href="javascript:;" class="batchIsread"  mId=<?php  xcho($v['id']) ?> ">
                                       <?php if($v['read_time']==0||empty($v['read_time'])) {?><span data-messageid="<?php  xcho($v['id']) ?>" class="set_read">设为已读</span><?php } ?>
                                        </a></td>

                                    </tr>
                                    <?php } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <div class="panel-footer">
                            <div class="top_pages bottom_pages">
                                <ul><?php xchoHtml($list->render())   ?></ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <a href="#" class="scroll-to-top"><i class="fa fa-angle-double-up"></i></a>
    </div>
</div>
<input type="hidden" name="batchIsRead" value="<?php xcho($batchIsRead) ?>"/>
<input type="hidden" name="delLetter" value="<?php xcho($delLetter) ?>"/>
<script src="<?php xcho(loadSource('/assets/plugins/jquery/jquery.min.js')) ?>"></script>
<script src="<?php xcho(loadSource('/assets/plugins/bootstrap/js/bootstrap.min.js')) ?>"></script>
<script src="<?php xcho(loadSource('/assets/plugins/data-table/media/js/jquery.dataTables.min.js')) ?>"></script>
<script src="<?php xcho(loadSource('/assets/plugins/data-table/media/js/dataTables.bootstrap.min.js')) ?>"></script>
<script
    src="<?php xcho(loadSource('/assets/plugins/data-table/extensions/Responsive/js/dataTables.responsive.min.js')) ?>"></script>
<script
    src="<?php xcho(loadSource('/assets/plugins/data-table/extensions/Responsive/js/responsive.bootstrap.min.js')) ?>"></script>
<script>
    $(function () {


        "use strict";

        //TABLE
        $("#checkAll").click(function () {
            $("input[name='ck']").each(function () {
                if ($(this).prop("checked")) {
                    $(this).prop("checked", false);
                }
                else {
                    $(this).prop("checked", "true");
                }
            })
        });
//        function selectAll(){
//            if ($("#SelectAll").attr("checked")) {
//                $(":checkbox").attr("checked", true);
//            } else {
//                $(":checkbox").attr("checked", false);
//            }
//        }

        $("#messageall").on("click", function () {
            localStorage.setItem("msgstatus", 1);
        });
        $("#notread").on("click", function () {
            localStorage.setItem("msgstatus", 2);
        });

        var msgStatus = localStorage.getItem("msgstatus");
        if(msgStatus == 1){
            $("#messageall").removeClass("btn-success");
            $("#messageall").addClass("btn-info");
            $("#notread").removeClass("btn-info");
            $("#notread").addClass("btn-success");
        }
        if(msgStatus == 2){
            $("#messageall").removeClass("btn-info");
            $("#messageall").addClass("btn-success");
            $("#notread").removeClass("btn-success");
            $("#notread").addClass("btn-info");
        }

        $("#read_messages").on("click", function () {
            var ids = [];
            $('input[name="ck"]:checked').each(function () {
                ids.push($(this).val());
                var id = $(this).val();
                $("tbody tr").each(function () {
                    console.log('$(this).attr("data-messageid")===' + $(this).attr("data-r"))
                    if ($(this).attr("data-r") == id) {
                        $(this).find("i.fa-envelope").fadeOut();
                    }
                });
                $(".set_read").each(function () {
                    console.log('$(this).attr("data-messageid")===' + $(this).attr("data-r"))
                    if ($(this).attr("data-messageid") == id) {
                        $(this).fadeOut();
                    }
                });
            });
            console.info(ids);
            if (ids.length == 0) {
                alert("至少选择一项");
            } else {
                $.ajax({
                    url: $("input[name='batchIsRead']").val(),
                    data: {
                        ids: ids,
                        read_status: "is_read"
                    },
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        console.log(data)
                        if(data.success == 1){
                            $("#no_read_message").html(data.count.noMsg);
                            $("#all_message").html(data.count.totalMsg);
                        }else{

                        }
                    },
                    complete:function(){
                        location.reload();
                    },
                    error: function () {

                    }
                });
            }

        });
        $("#del_messages").on("click", function () {
            var ids = [];
            $('input[name="ck"]:checked').each(function () {
                ids.push($(this).val());
                var id = $(this).val();
                $("tbody tr").each(function () {
                    console.log('$(this).attr("data-messageid")===' + $(this).attr("data-r"))
                    if ($(this).attr("data-r") == id) {
                        $(this).fadeOut();
                    }
                });
            });
            console.info(ids);
            if (ids.length == 0) {
                alert("至少选择一项");
            } else {
                $.ajax({
                    url: $("input[name='delLetter']").val(),
                    data: {
                        ids: ids,
                        read_status: "is_read"
                    },
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        console.log(data)
                        if(data.success == 1){
                            $("#no_read_message").html(data.count.noMsg);
                            $("#all_message").html(data.count.totalMsg);
                        }else{

                        }
                    },
                    complete:function(){
                        location.reload();
                    },
                    error: function () {

                    }
                });
            }

        });
        $(".set_read").on("click", function () {
            console.log($(this).attr("data-messageid"));
            var id = $(this).attr("data-messageid");
            var ids = [];
            ids.push(id);
            $.ajax({
                url: $("input[name='batchIsRead']").val(),
                data: {
                    ids: ids,
                    read_status: "is_read"
                },
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    console.log(data)
                    if(data.success==1){
                        $("tbody tr").each(function () {
                            console.log('$(this).attr("data-messageid")===' + $(this).attr("data-r"))
                            if ($(this).attr("data-r") == id) {
                                $(this).find("i.fa-envelope").fadeOut();
                            }
                        });
                        $(".set_read").each(function () {
                            console.log('$(this).attr("data-messageid")===' + $(this).attr("data-r"))
                            if ($(this).attr("data-messageid") == id) {
                                $(this).fadeOut();
                            }
                        });
                        if(data.success == 1){
                            $("#no_read_message").html(data.count.noMsg);
                            $("#all_message").html(data.count.totalMsg);
                        }else{

                        }
                    }
                },
                complete:function(){
                    location.reload();
                },
                error: function () {

                }
            });
        })
        $('.del_message').on("click", function () {
            console.log($(this).attr("data-messageid"));
            var ids = [];
            var id = $(this).attr("data-messageid");
            ids.push($(this).val());
            $.ajax({
                url: $("input[name='delLetter']").val(),
                data: {
                    ids: id,
                    read_status: "is_read"
                },
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    if (data.success) {
                        $("tbody tr").each(function () {
                            console.log('$(this).attr("data-messageid")===' + $(this).attr("data-r"))
                            if ($(this).attr("data-r") == id) {
                                $(this).fadeOut();
                            }
                        });
                        if(data.success == 1){
                            $("#no_read_message").html(data.count.noMsg);
                            $("#all_message").html(data.count.totalMsg);
                        }else{

                        }
                    } else {
                        alert("删除失败,请刷新重试!")
                    }

                },
                complete:function(){
                  location.reload();
                },
                error: function () {

                }
            });

        });
        $("input[type='checkbox']").on("click", function () {
            var shecked_length = $("input[name='ck']:checked").length;

        });


    });
</script>
<?php include_once TPL_PATH . 'public/foot_script.phtml'; ?>
<button id="top" style="height: 50px;width: 50px;"></button></body>

</html>
